package com.yzx.lifeassistants.model.impl;

import java.util.Properties;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import android.content.Context;

import com.yzx.lifeassistants.bean.ActivityInfo;
import com.yzx.lifeassistants.model.IQueryActivityInfo;
import com.yzx.lifeassistants.model.callback.IQueryActivityInfoCB;
import com.yzx.lifeassistants.utils.HttpClientUtil;
import com.yzx.lifeassistants.utils.IHttpCallBack;
import com.yzx.lifeassistants.utils.LogcatUtils;
import com.yzx.lifeassistants.utils.PropertyUtil;

/**
 * @Description: 查询活动详情信息
 * @author: yzx
 * @time: 2016-3-22 下午9:33:50
 */
public class QueryActivityInfoImpl implements IQueryActivityInfo {

	public static final String TAG = "QueryActivityInfoImpl";
	private IQueryActivityInfoCB queryCallBack;// 回调
	private String refererUrl;// URL

	public QueryActivityInfoImpl(Context context,
			IQueryActivityInfoCB queryCallBack) {
		this.queryCallBack = queryCallBack;
		Properties properties = PropertyUtil.getProperties(context,
				"base.properties");
		refererUrl = properties.getProperty("activity_referer_url");
	}

	/**
	 * 
	 * @Description: 查询招聘信息
	 */
	@Override
	public void queryActivityInfo(String url) {
		HttpClientUtil.sendRequestWithGBK(url, null, new IHttpCallBack() {

			@Override
			public void onFinish(String response) {
				LogcatUtils.i(TAG, response);
				ActivityInfo activityInfo = new ActivityInfo();
				if (null == response || "".equals(response)) {
					LogcatUtils.e(TAG, "获取活动详情失败");
					queryCallBack.queryError(IQueryActivityInfoCB.RESULR_NULL);
				} else {
					Document doc = Jsoup.parse(response);
					Element titleElement = doc.select(
							"div.detail_main_content>h3").first();
					String title = titleElement.text();
					if (haveData(title)) {
						activityInfo.setTitle(title);
						LogcatUtils.i(TAG, title);
					}
					Element contentElement = doc.select(
							"div.detail_content_display>div").first();
					String content = contentElement.toString();
					while (content.contains("src=\"/attach")) {
						int start = content.lastIndexOf("src=\"/attach");
						String after = content.substring(start + 5,
								content.length());
						String before = content.substring(0, start + 5);
						content = before + refererUrl + after;
						LogcatUtils.i(TAG,
								content.substring(start, content.length()));
					}
					LogcatUtils.i(TAG, content);
					if (haveData(content)) {
						activityInfo.setContent(content);
					}
					if (haveData(activityInfo.getContent())
							&& haveData(activityInfo.getTitle())) {
						LogcatUtils.i(TAG, "获取活动详情成功");
						queryCallBack.querySuccess(activityInfo);
					} else {
						LogcatUtils.e(TAG, "获取活动详情失败");
						queryCallBack
								.queryError(IQueryActivityInfoCB.RESULR_NULL);
					}
				}
			}

			@Override
			public void onError(Exception e) {
				LogcatUtils.e(TAG, "获取活动详情异常");
				if (null != e.getMessage()) {
					LogcatUtils.e(TAG, e.getMessage());
				}
				queryCallBack.queryError(IQueryActivityInfoCB.QUERY_ERROR);
			}
		});
	}

	/**
	 * 
	 * @Description: 是否有数据
	 */
	private Boolean haveData(String data) {
		if (null == data || "".equals(data)) {
			return false;
		} else {
			return true;
		}
	}
}
